<?php

include('../_libs/mysql.php');
if(!isset($_SESSION['access_level'])) {
	$_SESSION['access_level'] = 0;
}
if($_SESSION['access_level'] >= 1) {
if (empty($_GET['get'])) {
	$term = trim(strip_tags($_GET['term'])); //retrieve the search term that autocomplete sends

	$sql = 'SELECT id, name, email FROM ' . DB_LOGIN . '.account_data WHERE name LIKE :search OR id LIKE :search OR email LIKE :search';
	$dbh = $connection->prepare($sql);
	$dbh->bindValue(':search', "%{$term}%", PDO::PARAM_STR);
	$dbh->execute();
	while ($row = $dbh->fetch(PDO::FETCH_ASSOC)) {//loop through the retrieved values
		$row['value'] = htmlentities(stripslashes($row['name']));
		$row['id'] = (int) $row['id'];
		$row_set[] = $row; //build an array
	}
	echo json_encode($row_set); //format the array into json data
} else {
	$term = trim(strip_tags($_GET['term'])); //retrieve the search term that autocomplete sends

	$sql = 'SELECT id, name FROM ' . DB_GAME . '.players WHERE name LIKE :search OR id LIKE :search';
	$dbh = $connection->prepare($sql);
	$dbh->bindValue(':search', "%{$term}%", PDO::PARAM_STR);
	$dbh->execute();
	while ($row = $dbh->fetch(PDO::FETCH_ASSOC)) {//loop through the retrieved values
		$row['value'] = htmlentities(stripslashes($row['name']));
		$row['id'] = (int) $row['id'];
		$row_set[] = $row; //build an array
	}
	echo json_encode($row_set); //format the array into json data
}
}
?>
